DAY36:Maximum subarray sum


Posted by birdbirdmurmur on 2023-08-18

題目連結

https://www.codewars.com/kata/54521e9ec8e60bc4de000d6c

解法

function maxSequence(arr) {
  let maxSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let currentSum = 0;
    for (let j = i; j < arr.length; j++) {
      currentSum += arr[j];
      maxSum = Math.max(maxSum, currentSum);
    }
  }
  return maxSum;
}

筆記

使用雙重迴圈
比較當前sum和maxSum
最後回傳maxSum

[-2, 1, -3, 4, -1, 2, 1, -5, 4]


#javascript #Codewars







Related Posts

From Nand To Tetris:想理解電腦運作,就先做出一台吧!

From Nand To Tetris:想理解電腦運作,就先做出一台吧!

React 的 batch update 策略,包含 React 18 和 hooks

React 的 batch update 策略,包含 React 18 和 hooks

計算程式執行時間

計算程式執行時間


Comments